<!-- 充值 -->
<template>
  <div class="transferOut">
    <x-header :left-options="{backText: ''}">提现</x-header>
    <div class="transferOutAddress">选择提现地址</div>
    <div class="transferOutBox">
      <div class="address" @click="choiseAddress()">
        <img src="../../assets/qrCode.png" alt="" width="20" height="20" style="vertical-align: middle;">
        <span style="vertical-align: middle;" v-if="!typeName">我的地址</span>
        <span style="vertical-align: middle;" v-if="typeName">{{typeName}}</span>
        <img src="../../assets/nav.png" alt="" width="10" class="transferOutNav">
      </div>
      <div class="address">
        <span style="vertical-align: middle;">Token</span>
        <div class="token">
          <!-- <img src="../../assets/ethWallet.png" alt="" width="30" height="30" style="vertical-align: middle;"> -->
          <span style="vertical-align: middle;">{{type}}</span>
        </div>
      </div>
      <div class="transferOutMoney">
        <div style="height:10px;background: #f6f6f6"></div>
        <span class="transferOutMoneyNum">提现金额</span>
        <input placeholder="请输入提现金额" class="moneyNum" style="position: absolute;right: 15px; display: inline-block;height: 40px;line-height: 40px;border:none;text-align: right;border-style: none;outline: none;width:70%"
          v-model="amount">
      </div>
    </div>
    <div style="padding: 0px 15px;margin-top:130px;">
      <div class="transferOutConfim" @click="confimTransfer()">
        确认提现
      </div>
    </div>
    <!-- 输入支付密码 -->
    <div class="tips" v-show="showTips">
      <div class="tips-top">
        <div style="text-align: center;height: 45px;line-height: 45px;border-bottom: 1px solid #f6f6f6;">
          <img src="../../assets/close.png" alt="" width="20" @click="closeShowTips">
          <span class="c3">请输入支付密码</span>
        </div>
        <div>
          <div class="tips-center">
            <li>
              <input type="password" v-model="pwd1" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd2" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd3" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd4" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd5" maxlength="1" @keyup="checkPwd" class="input">
            </li>
            <li>
              <input type="password" v-model="pwd6" maxlength="1" @keyup="checkPwd" class="input">
            </li>
          </div>
          <div class="forgetPassWord" @click="$router.push({path:'/main/forgetPay'})">忘记密码?</div>
        </div>
      </div>
    </div>
    <!-- 支付密码错误弹框 -->
    <div class="errTips" v-if="errTips">
      <div class="errTipsBox">
        <div class="errTips-top">
          支付密码错误,请重试!
        </div>
        <div class="errTipsBtn">
          <span class="active" @click="retry">重试</span>
          <span @click="$router.push({path:'/main/forgetPay'})">忘记密码?</span>
        </div>
        <div style="margin-left: 30px;font-size: 13px;margin-top: 5px;color: #ff1010">(说明：只能重试3次)</div>
      </div>
    </div>
    <!-- 设置支付密码 -->
    <div class="setPassWorldTips" v-if="isPassWorld">
      <div class="setPasswordText">
        <div class="setPassWorldTitle">请设置支付密码!</div>
        <div class="goPassworld" @click="$router.push({path:'/main/setPayPassword'})">立即前往</div>
      </div>
    </div>
  </div>
</template>
<script>
  import { XHeader } from 'vux'
  import http from '../../service/http.js'
  import tips from '../../service/tips.js'
  import util from '../../service/util.js'
  export default {
    data() {
      return {
        type: "",
        amount: "",
        id: "",
        showTips: false,//显示支付密码弹框
        errTips: false,//显示支付错误弹框
        isPassWorld: false,//判断有没有支付密码
        pwd: "",
        pwd1: "",
        pwd2: "",
        pwd3: "",
        pwd4: "",
        pwd5: "",
        pwd6: "",
        typeName: "",
      }
    },
    components: {
      XHeader,
    },
    mounted() {
      this.id = this.$route.query.id;
      this.typeName = this.$route.query.name;
      this.type = this.$route.query.type;
      var txts = document.querySelectorAll(".input");
      for (var i = 0; i < txts.length; i++) {
        var t = txts[i];
        t.index = i;
        t.setAttribute("readonly", true);
        t.onkeyup = function () {
          this.value = this.value.replace(/^(.).*$/, '$1');
          var next = this.index + 1;
          if (next > txts.length - 1) return;
          txts[next].removeAttribute("readonly");
          txts[next].focus();
        }
      }
      txts[0].removeAttribute("readonly");
    },
    methods: {
      // 关闭支付密码输入弹框
      closeShowTips() {
        this.showTips = false;
        this.pwd1 = ""
        this.pwd2 = ""
        this.pwd3 = ""
        this.pwd4 = ""
        this.pwd5 = ""
        this.pwd6 = ""
      },
      // 重试按钮
      retry() {
        this.errTips = false;
        this.showTips = true;
        this.pwd1 = ""
        this.pwd2 = ""
        this.pwd3 = ""
        this.pwd4 = ""
        this.pwd5 = ""
        this.pwd6 = ""
      },
      //判断有无支付密码
      hasPay() {
        http.post('/user/hasPay', {
          username: window.localStorage.getItem("login.username")
        }).then((res) => {
          if (res.status == 200 && !res.data.body) {//无支付密码
            this.isPassWorld = true;
          }
          if (res.status == 200 && res.data.body) { //有支付密码(下单)
            this.showTips = true;
          }
        })
      },
      //校验支付密码
      checkPwd() {
        this.pwd = this.pwd1 + this.pwd2 + this.pwd3 + this.pwd4 + this.pwd5 + this.pwd6;
        if (this.pwd1 && this.pwd2 && this.pwd3 && this.pwd4 && this.pwd5 && this.pwd6) {
          http.post('/user/authPay', {
            payPwd: util.md5(this.pwd)
          }).then((res) => {
            if (res.data.status == 200) {
              http.put("/user/coin/cash", {
                payAmount: this.amount,
                id: this.id,
                name: this.type,
              }).then((res) => {
                if (res.data.status == 200) {
                  this.$router.push({ path: "/main/applicationCash" })
                } else {
                  this.showTips = false;
                  this.errTips = true;
                  this.pwd1 = ""
                  this.pwd2 = ""
                  this.pwd3 = ""
                  this.pwd4 = ""
                  this.pwd5 = ""
                  this.pwd6 = ""
                  tips.tips(res.data.msg)
                }
              }).catch((res) => {
                this.showTips = false;
                this.errTips = true;
                this.pwd1 = ""
                this.pwd2 = ""
                this.pwd3 = ""
                this.pwd4 = ""
                this.pwd5 = ""
                this.pwd6 = ""
                tips.tips(res.data.msg)
              })
            } else {
              this.showTips = false;
              this.errTips = true;
              this.pwd1 = ""
              this.pwd2 = ""
              this.pwd3 = ""
              this.pwd4 = ""
              this.pwd5 = ""
              this.pwd6 = ""
            }
          }).catch((res) => {
            tips.tips(res.data.msg)
          })
        }
      },
      // 确认提现
      confimTransfer() {
        if (!this.id) {
          tips.tips("请选择提现地址");
          return false;
        }
        if (!this.amount) {
          tips.tips("请输入提现金额");
          return false;
        }
        this.hasPay();
      },
      choiseAddress() {
        this.$router.push({ path: "/main/select?type=" + this.type })
      }
    }
  }
</script>
<style lang="less">
  body {
    background: #f6f6f6;
  }

  .transferOut .vux-header {
    background: #fff;
  }

  .transferOut .vux-header .vux-header-title {
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    font-weight: 400;
    color: #000;
  }

  .transferOut .vux-header .vux-header-left .left-arrow:before {
    content: "";
    position: absolute;
    width: 12px;
    height: 12px;
    border: 1px solid #000;
    border-width: 1px 0 0 1px;
    -webkit-transform: rotate(315deg);
    transform: rotate(315deg);
    top: 8px;
    left: 7px;
  }

  .transferOut .transferOutAddress {
    height: 30px;
    line-height: 30px;
    padding-left: 15px;
    color: #999;
  }

  .transferOut .transferOutBox {
    background: #fff;
  }

  .transferOut .transferOutBox .address {
    position: relative;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #f6f6f6;
    padding: 0px 15px;
  }

  .transferOut .transferOutBox .address .transferOutNav {
    position: absolute;
    right: 15px;
    top: 15px;
  }

  .transferOut .transferOutBox .address .token {
    position: absolute;
    right: 15px;
    top: 0px;
  }

  .transferOut .transferOutMoneyNum {
    height: 40px;
    line-height: 40px;
    display: inline-block;
    padding: 0 15px;
  }

  .transferOutMoney {
    position: relative;
  }

  .transferOutConfim {
    height: 40px;
    text-align: center;
    line-height: 40px;
    border-radius: 25px;
    /* background: linear-gradient(left, #fedb30, #fe9f0a); */
    background: -webkit-gradient(linear,left top,right top,from(#fedd32),to(#fe9e09));
    color: #fff;
  }


  .transferOut .tips {
    position: fixed;
    background: rgba(0, 0, 0, 0.3);
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    z-index: 999;
    .tips-top {
      position: absolute;
      top: 40%;
      height: 60%;
      background: #fff;
      width: 100%;
      border-bottom: 1px solid #f6f6f6;
      img {
        position: absolute;
        left: 15px;
        top: 15px;
      }
    }
  }

  .transferOut .tips-center {
    padding-left: 10%;
    margin-top: 45px;
  }

  .transferOut .tips-center li {
    float: left;
    width: 15%;
    list-style: none;
    height: 45px;
    line-height: 45px;
    border: 1px solid #ccc;
    border-right: none;
    overflow: hidden;
    text-align: center;
    input {
      border: none;
      outline: none;
      height: 45px;
      line-height: 45px;
      width: 100%;
      display: block;
      text-align: center;
    }
  }

  .transferOut .tips-center li:last-child {
    border-right: 1px solid #ccc;
  }

  .transferOut .forgetPassWord {
    position: absolute;
    right: 28px;
    top: 150px;
    color: #fea620;
  }

  .transferOut .errTips {
    position: fixed;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .transferOut .errTipsBox {
    position: absolute;
    top: 40%;
    width: 90%;
    height: 130px;
    background: #fff;
    margin-left: 5%;
    border-radius: 15px;
    .errTips-top {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
    }
    .errTipsBtn {
      span {
        display: inline-block;
        width: 48%;
        text-align: center;
        margin-top: 15px;
        color: #fea620;
      }
      .active {
        border-right: 1px solid #efefef;
      }
    }
  }

  .transferOut .setPassWorldTips {
    position: fixed;
    top: 0px;
    left: 0px;
    bottom: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.3);
    z-index: 999;
  }

  .transferOut .setPasswordText {
    position: absolute;
    top: 40%;
    height: 130px;
    background: #fff;
    width: 90%;
    margin-left: 5%;
    border-radius: 15px;
    .setPassWorldTitle {
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #efefef;
      color: #000;
    }
    .goPassworld {
      height: 70px;
      line-height: 65px;
      color: #fea620;
      text-align: center;
    }
  }
</style>